package jee.boot.entity.task;

import com.fasterxml.jackson.annotation.JsonFormat;
import jee.boot.common.annotation.Column;
import jee.boot.common.annotation.Table;
import jee.boot.common.annotation.Transient;
import jee.boot.common.basic.DataEntity;
import jee.boot.entity.file.Attachment;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

/**
 * 导入导出任务
 */
@Table(name = "st_iexp_task")
public class IexpTask extends DataEntity<IexpTask> {
    /**
     * 任务名称
     */
    private String name;
    /**
     * 乐观锁
     */
    private Integer version = 1;
    /**
     * 服务器名称
     */
    private String computerName;
    /**
     * 委托服务类,
     */
    private String delegatedClass;
    /**
     * 任务类型: iexp_type_imp:导入;iexp_type_exp:导出,
     */
    private String iexpType;
    /**
     * 任务状态: iexp_status_success:成功;iexp_status_error:失败;iexp_status_new:新建;iexp_status_running:运行,
     */
    private String iexpStatus;
    /**
     * 文件路径,
     */
    private String fileName;
    /**
     * 文件拓展名,
     */
    private String fileExt;
    /**
     * 开始时间,
     */
    private Date startDate;
    /**
     * 结束时间,
     */
    private Date endDate;
    /**
     * 详情,
     */
    private String message;
    /**
     * json查询参数,
     */
    private String jsonParam;
    /**
     * 任务附件以这个为准了
     */
    @Column(data = {"attachment.id", "attachment.fileId", "attachment.fileName", "attachment.fileType", "attachment.savePath", "attachment.contentType"})
    private Attachment attachment;
    /**
     * 线程优先级,
     */
    private int priority;
    /**
     * 如果是big就放进大任务的线程池,
     */
    private String bigOrNormal = "normal";
    /**
     * 引用URI,告知后台服务线程,当前的任务是从哪个URI中触发的,用于识别数据权限
     */
    private String refererUri;
    /**
     * 多语言,告知后台服务器,当前任务中使用的语言环境是什么,一般取自触发任务的请求中的head中定义的Language
     */
    private String language;
    /**
     * 异常详情列表
     */
    @Transient
    private List<IexpTaskItem> itemList;


    /**
     * 生成一个后台标准任务
     *
     * @param delegatedClass 必填,导出任务的委托类,需要实现jee.boot.module.iexp.task.ExportExecuteService,值可以是全路径,也可以是spring容器中的类别名
     * @param jsonParam      选填,导出数据的查询条件
     * @return
     */
    public static IexpTask createNewStandardTask(String delegatedClass, String jsonParam) {
        IexpTask task = new IexpTask();
        task.setIexpType("iexp_type_standard");
        task.setIexpStatus("iexp_status_new");
        try {
            task.setComputerName(InetAddress.getLocalHost().getHostName());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        task.setDelegatedClass(delegatedClass);
        task.setJsonParam(jsonParam);
        return task;
    }

    /**
     * 生成一个后台导出任务
     *
     * @param delegatedClass 必填,导出任务的委托类,需要实现jee.boot.module.iexp.task.ExportExecuteService,值可以是全路径,也可以是spring容器中的类别名
     * @param jsonParam      选填,导出数据的查询条件
     * @return
     */
    public static IexpTask createNewExpTask(String delegatedClass, String jsonParam) {
        IexpTask task = new IexpTask();
        task.setIexpType("iexp_type_exp");
        task.setIexpStatus("iexp_status_new");
        try {
            task.setComputerName(InetAddress.getLocalHost().getHostName());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        task.setDelegatedClass(delegatedClass);
        task.setJsonParam(jsonParam);
        return task;
    }

    /**
     * 生成一个后台导入任务
     *
     * @param delegatedClass 必填,导入任务的委托类,需要实现jee.boot.module.iexp.task.ImportExecuteService,值可以是全路径,也可以是spring容器中的类别名
     * @param jsonParam      选填,导入的时候设置的默认参数,比如headerNum: 导入文件的head行数,sheetIndexOrName: 导入文件的sheet页序列或者名称等
     * @param fileName       必填,导入文件的文件名
     * @return
     */
    public static IexpTask createNewImpTask(String delegatedClass, String jsonParam, String fileName) {
        IexpTask task = new IexpTask();
        task.setIexpType("iexp_type_imp");
        task.setIexpStatus("iexp_status_new");
        try {
            task.setComputerName(InetAddress.getLocalHost().getHostName());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        task.setDelegatedClass(delegatedClass);
        task.setJsonParam(jsonParam);
        task.setFileName(fileName);
        return task;
    }

    public IexpTask() {
    }

    public IexpTask(String id) {
        super(id);
    }

    public String getDelegatedClass() {
        return delegatedClass;
    }

    public void setDelegatedClass(String delegatedClass) {
        this.delegatedClass = delegatedClass;
    }


    public String getIexpType() {
        return iexpType;
    }

    public void setIexpType(String iexpType) {
        this.iexpType = iexpType;
    }

    public String getIexpStatus() {
        return iexpStatus;
    }

    public void setIexpStatus(String iexpStatus) {
        this.iexpStatus = iexpStatus;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileExt() {
        return fileExt;
    }

    public void setFileExt(String fileExt) {
        this.fileExt = fileExt;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getJsonParam() {
        return jsonParam;
    }

    public void setJsonParam(String jsonParam) {
        this.jsonParam = jsonParam;
    }

    public List<IexpTaskItem> getItemList() {
        return itemList;
    }

    public void setItemList(List<IexpTaskItem> itemList) {
        this.itemList = itemList;
    }

    public String getComputerName() {
        return computerName;
    }

    public void setComputerName(String computerName) {
        this.computerName = computerName;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public String getBigOrNormal() {
        return bigOrNormal;
    }

    public void setBigOrNormal(String bigOrNormal) {
        this.bigOrNormal = bigOrNormal;
    }

    public Attachment getAttachment() {
        return attachment;
    }

    public void setAttachment(Attachment attachment) {
        this.attachment = attachment;
    }

    public String getRefererUri() {
        return refererUri;
    }

    public void setRefererUri(String refererUri) {
        this.refererUri = refererUri;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }
}
